package system.manage;

import groupby.manage.GroupbyManageImpl;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import system.model.SystemInfor;

public class SystemManageImpl implements SystemManage {

	public String getConfig() {
		
          
		return null;
	}
/**
 * 获得system infor
 */
	public SystemInfor systemInfor(String path) {
		SystemInfor systemInfor=new SystemInfor();
		
    	path=path.replaceAll("%20"," ").replace("/", File.separator).replace("\\",File.separator);
    	
    	
    	
    	File file=new File(path);
    	
    	SAXReader reader = new SAXReader();
    	String tags=null;
    	Document doc;
    	try {
    		 doc = reader.read(file);
    		 systemInfor.setLogo(doc.selectSingleNode("//system/logo").getText().trim());
    		 systemInfor.setWebName(doc.selectSingleNode("//system/webName").getText().trim());
    		 systemInfor.setBottomContent(doc.selectSingleNode("//system/bottomContent").getText().trim());
    		 systemInfor.setStartTime(doc.selectSingleNode("//system/startTime").getText().trim());
    		 systemInfor.setEndTime(doc.selectSingleNode("//system/endTime").getText().trim());
    		 systemInfor.setWebEmail(doc.selectSingleNode("//system/webEmail").getText().trim());
    		 systemInfor.setWebEmailPass(doc.selectSingleNode("//system/webEmailPass").getText().trim());
    		 List listTemp=new ArrayList();
    		 List list=doc.selectNodes("//system/ads/ad");
			 for(int j=0;j<list.size();j++)
			 {
				 Element element=(Element)list.get(j);
				 listTemp.add(element.getText()+","+element.attributeValue("url"));
				 
			 }
			 systemInfor.setAdImage(listTemp);
    		
    	} catch (DocumentException e) {
    		
    		e.printStackTrace();
    	}
		return systemInfor;
	}
	/**
	 * 添加system infor
	 */
public void addSystemInfor(SystemInfor systemInfor,String path) {
	path=path.replaceAll("%20"," ").replace("/", File.separator).replace("\\",File.separator);
	File file=new File(path);
	
	SAXReader reader = new SAXReader();
	reader.setEncoding("utf-8"); 
	String tags=null;
	Document doc;
	try {
		 doc = reader.read(file);
		 doc.selectSingleNode("//system/logo").setText(systemInfor.getLogo());
		doc.selectSingleNode("//system/webName").setText(systemInfor.getWebName());
		 doc.selectSingleNode("//system/bottomContent").setText(systemInfor.getBottomContent());
		 doc.selectSingleNode("//system/startTime").setText(systemInfor.getStartTime());
		doc.selectSingleNode("//system/endTime").setText(systemInfor.getEndTime());
		doc.selectSingleNode("//system/webEmail").setText(systemInfor.getWebEmail());
		doc.selectSingleNode("//system/webEmailPass").setText(systemInfor.getWebEmailPass());
		
		
		 OutputFormat format = new OutputFormat("\t", true); 
         format.setEncoding("utf-8");   
          XMLWriter output;
		 output = new XMLWriter(new FileOutputStream(new File(path)), format);
		 output.write(doc);   
           output.close(); 
           doc.clearContent();
		
	} catch (Exception e) {
		
		e.printStackTrace();
	}
	
	
}
/**
 * 添加广告位
 */
	public void addAd(SystemInfor systemInfor, String[] adImage,String path,String[] url) {
		path=path.replaceAll("%20"," ").replace("/", File.separator).replace("\\",File.separator);
		File file=new File(path);
		
		SAXReader reader = new SAXReader();
		reader.setEncoding("utf-8"); 
		String tags=null;
		Document doc;
		try {
			 doc = reader.read(file);
			 for(int i=0;i<systemInfor.getAdImage().size();i++)
			 {
				 String imageUrl=(String)systemInfor.getAdImage().get(i);
				 List list=doc.selectNodes("//system/ads/ad");
				 for(int j=0;j<list.size();j++)
				 {
					 Element element=(Element)list.get(j);
					 if(element.attribute("id").getValue().equals(adImage[i]))
					 {
						 element.setText(imageUrl);
						 element.setAttributeValue("url", url[i]);
						 break;
					 }
					 
				 }
			 }
			 OutputFormat format = new OutputFormat("\t", true); 
			 format.setEncoding("utf-8");   
	          XMLWriter output;
			 output = new XMLWriter(new FileOutputStream(new File(path)), format);
			 output.write(doc);   
	           output.close(); 
	           doc.clearContent();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}

}
